//DON'T WORK BECAUSE AJAX sends request only to the same domain

dojo.provide("dojo.app.myvalidator.validators.remoteUrl");

dojo.declare("dojo.app.myvalidator.validators.remoteUrl", null, {
    _elemValue : null,
    _element : null,
    constructor: function() {
      this._elemValue = {};
      this._element = {};
    },
    setElementValue: function(elementValue) {
        this._elemValue = elementValue;
    },
    setElement: function(element) {
        this._element = element;
    },
    getErrorMessage: function(locale, messagesCur) {
        dojo.mixin(this.errorMessages, messagesCur);
        return this.errorMessages[locale];
    },

    //Type here default error message for this validator. It's could be replaced
    //by validation options for every used element.
    //You can use %value% to place a current value of validated element.
    //You can use %neededValue% to place a value that is compared with %value%
    errorMessages: {"en" : "Remote Url is inaccessible",
                    "ru" : "Удаленный ресурс недоступен или не существует"},
    validate:
        //Type here your validation function
        //Use this._elemValue as a value of your validated element
        //    this._elemValue - is an object!!!
        //    neededValue as a value to compare with this._elemValue
        //Function must returns true if validation is not successful
        //                      false otherwise
        function(neededValue) {
            var errorStatus = false;

            dojo.xhrGet({
                url: this._elemValue,
                sync: true,
                load: function (data, ioArgs) {
                    console.log(ioArgs);
                          if (ioArgs != "success" || data.length == 0) {
                              errorStatus = true;
                          };
                      },
                error: function() {
                           errorStatus = true;
                       }
             });
             
             if (errorStatus) {
                 return true;
             } else {
                 return false;
             };
    }
});

